package org.jeecg.modules.cusorder.service.impl;

import java.math.BigDecimal;
import java.util.List;

import org.jeecg.modules.cusorder.entity.CusOrderInfo;
import org.jeecg.modules.cusorder.mapper.CusOrderInfoMapper;
import org.jeecg.modules.cusorder.service.ICusOrderInfoService;
import org.jeecg.modules.cusstyle.entity.CusStyleInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * @Description: 订单信息
 * @Date:   2021-09-10
 * @Version: V1.0
 */
@Service
public class CusOrderInfoServiceImpl extends ServiceImpl<CusOrderInfoMapper, CusOrderInfo> implements ICusOrderInfoService {

	@Autowired
	private CusOrderInfoMapper cusOrderInfoMapper;
	
	/**
	 * 通过订单ID查询是否有选款数量
	 */
	@Override
	public boolean checkOrderStyle(String orderInfoId) {
		Integer count = cusOrderInfoMapper.countAllOrderStyle(orderInfoId);
		if(count > 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 通过订单ID查询款式转生产单的数量
	 */
	@Override
	public Integer countConvertedOrderStyle(String orderInfoId) {
		return cusOrderInfoMapper.countConvertedOrderStyle(orderInfoId);
	}

	/**
	 * 通过订单ID更新订单状态
	 */
	@Override
	public void updateStatus(String orderInfoId, Integer status) {
		UpdateWrapper<CusOrderInfo> updateWrapper = new UpdateWrapper<>();
		updateWrapper.eq("id", orderInfoId);
		CusOrderInfo cusOrderInfo = new CusOrderInfo();
		cusOrderInfo.setStatus(status);
		cusOrderInfoMapper.update(cusOrderInfo, updateWrapper);
	}

	/**
	 * 根据订单ID查询客户款式列表
	 */
	@Override
	public List<CusStyleInfo> getCusStyleInfoListByOrderId(String orderInfoId) {
		List<CusStyleInfo> list = cusOrderInfoMapper.getCusStyleInfoListByOrderId(orderInfoId);
		return list;
	}

	/**
	 * 批量取消订单
	 */
	@Override
	public void cancelByIds(List<String> idsList, String status) {
		cusOrderInfoMapper.cancelByIds(idsList,status);
	}

	/**
	 * 更新订单的价格、下单数量、做货数量等信息
	 */
	@Override
	public boolean updateOrderInfo(String orderInfoId) {
		
		CusOrderInfo cusOrderInfo = this.getById(orderInfoId);//订单信息
		List<CusStyleInfo> cusStyleInfoList = this.getCusStyleInfoListByOrderId(orderInfoId);//查出当前订单下的所有款式信息列表
		int totalCount = 0;//订单信息中的总下单数量
		BigDecimal totalMoney = new BigDecimal("0");//合计订单总价
		for(CusStyleInfo cusStyleInfo : cusStyleInfoList) {
			Integer count = cusStyleInfo.getCount();//下单数量
			totalCount = (count != null) ? (totalCount + count) : totalCount;
			BigDecimal unitPrice = cusStyleInfo.getUnitPrice();//单价
			BigDecimal labourCost = cusStyleInfo.getLabourCost();//工费
			BigDecimal electroplateCost = cusStyleInfo.getElectroplateCost();//电镀费
			
			BigDecimal allUnitPrice = new BigDecimal("0");
			allUnitPrice = unitPrice != null ? allUnitPrice.add(unitPrice) : allUnitPrice;//合计单价（单价+工费+电镀费）
			if(labourCost != null) {
				allUnitPrice = allUnitPrice.add(labourCost);
			}
			if(electroplateCost != null) {
				allUnitPrice = allUnitPrice.add(electroplateCost);
			}
			totalMoney = totalMoney.add(allUnitPrice.multiply(new BigDecimal(String.valueOf(count))));//计算加上该款式后的订单总价
		}
		BigDecimal disCountRate = cusOrderInfo.getDiscountRate();//折扣率
		BigDecimal disCount = totalMoney.multiply(disCountRate).divide(new BigDecimal("100")).setScale(2, BigDecimal.ROUND_UP);//折后总价:四舍五入，小数保留2位。
		UpdateWrapper<CusOrderInfo> updateWrapper = new UpdateWrapper<>();
		updateWrapper.set("money", totalMoney);//订单总价
		updateWrapper.set("discount",disCount);//折后总价
		updateWrapper.set("count", totalCount);//订单信息中的总下单数量
		updateWrapper.eq("id", orderInfoId);//订单主键ID
		return this.update(updateWrapper);//更新订单的价格信息
	}
}
